<template>
	<el-form ref="form" label-width="80px" style="margin: 20px" size="mini" label-position="left">
	  <el-form-item label="账户名称">
	    <el-input v-model="name"></el-input>
	  </el-form-item>

	  <el-form-item label="账户编号">
	    <el-input v-model="number"></el-input>
	  </el-form-item>

	  <el-form-item label="账户类型">
	    <el-select v-model="type" placeholder="请选择账户类型">
	      <el-option
				v-for="(t, k) in accos"
				:label="t.name"
				:value="t.type"
                :key="k">
			</el-option>
	    </el-select>
	  </el-form-item>

	  <el-form-item label="账户卡号">
	    <el-input v-model="cardsn"></el-input>
	  </el-form-item>

	  <el-form-item label="初始金额" v-show="show">
	    <el-input v-model="money"></el-input>
	  </el-form-item>
	
	  <el-form-item label="账户描述">
	    <el-input type="textarea" v-model="remarks"></el-input>
	  </el-form-item>

	  <el-form-item>
	    <el-button type="primary" @click="submit">保存</el-button>
	    <el-button @click="close">取消</el-button>
	  </el-form-item>
	  
	</el-form>

</template>
<script>
import C from '@/common/common.js'
import Account from '@/service/shop/account.js'

export default {
  name: 'addAccount',
  data () {
    return {
      accos: [{name: '现金', type: '1'}, {name: '银行卡', type: '2'}, {name: '网银', type: '3'}],
      id: 0,
      name: '',
      type: '',
      cardsn: '',
      money: '',
      number: '',
      remarks: '',
      show: true
    }
  },
  methods: {
    async submit () {
      if (/^\s+$/.test(this.name)) {
        C.error('账户名称不能有空格！')
        return false
      } else if (this.number === '') {
        C.error('账户编号不能为空')
        return false
      } else if (this.type === '') {
        C.error('请选择账户类型！')
        return false
      }

      let data = {
        id: this.id,
        name: this.name,
        number: this.number,
        type: this.type,
        accsn: this.cardsn,
        money: this.money,
        remarks: this.remarks
      }
      let res = await Account.addEditAccount(data)
      C.closeBrowserWindow(res)
    },
    close () {
      C.closeBrowserWindow()
    }
  },
  mounted: function () {
    let row = C.getDatas()
    if (row.id) {
      this.id = row.id
      this.name = row.name
      this.number = row.number
      this.type = row.accounttype + ''
      this.cardsn = row.accountsn
      this.show = false
      this.remarks = row.remarks
    }
  }
}
</script>